package com.atguigu.seckill.service.serviceImp;


import com.atguigu.seckill.exception.GlobalException;
import com.atguigu.seckill.mapper.UserMapper;
import com.atguigu.seckill.pojo.User;
import com.atguigu.seckill.service.IUserService;
import com.atguigu.seckill.util.*;
import com.atguigu.seckill.vo.CodeResponse;
import com.atguigu.seckill.vo.CodeResponseEnum;
import com.atguigu.seckill.vo.LoginVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjj
 * @since 2021-04-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public CodeResponse loginService(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile=loginVo.getMobile();
        String password =loginVo.getPassword();
        //校验电话号，之前前端进行过校验，但后端校验是必不可少的
        /*if(StringUtils.isEmpty(password)|| StringUtils.isEmpty(mobile)){
            return CodeResponse.error(CodeResponseEnum.LOGIN_ERROR);
        }
        if(!ValidatorUtil.isMobile(mobile)){
            //不是电话号码
            return CodeResponse.error(CodeResponseEnum.MOBILE_ERROR);
        }*/
        //通过手机号查询用户信息
        User user=userMapper.selectById(mobile);
        //此处判空，否则报错
        if(null==user){
             //return  CodeResponse.error(CodeResponseEnum.LOGIN_ERROR);
            throw new GlobalException(CodeResponseEnum.LOGIN_ERROR);
        }

        //校验密码,这次是从一级密码向二级加密
        if(!MD5Utils.frompassToLastPass(password,user.getSalt()).equals(user.getPassward())){
            throw  new GlobalException(CodeResponseEnum.LOGIN_ERROR);
        }

        //生成Cookie的值，同时给session也赋值
        String ticket = UUIDUtil.uuid();
        //将user对象直接存到sredi中
        redisTemplate.opsForValue().set("user:"+ticket, JsonUtil.object2JsonStr(user));
        //request.getSession().setAttribute(ticket,user);  //这个是将对象存到session中
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return CodeResponse.success(ticket);
    }

    /**
     * 根据cookie获取用户
     *
     */
    @Override
    public User getByUserTicket(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
        String  userJson = (String) redisTemplate.opsForValue().get("user:" + userTicket);
        User user = JsonUtil.jsonStr2Object(userJson, User.class);
        if(user!=null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }


}
